import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Main from '../views/Main.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { isPublic: true }
  },
  {
    path: '/',
    name: 'Main',
    component: Main,
    children: [
      {
        path: 'categories/create',
        component: () => import(/* webpackChunkName: "CategoriesEdit" */ '../views/CategoriesEdit.vue')
      },
      {
        path: 'categories/edit/:id',
        component: () => import(/* webpackChunkName: "CategoriesEdit" */ '../views/CategoriesEdit.vue'),
        props: true
      },
      {
        path: 'categories/list',
        component: () => import(/* webpackChunkName: "CategoriesList" */ '../views/CategoriesList.vue')
      },
      {
        path: 'items/create',
        component: () => import(/* webpackChunkName: "ItemsEdit" */ '../views/ItemsEdit.vue')
      },
      {
        path: 'items/edit/:id',
        component: () => import(/* webpackChunkName: "ItemsEdit" */ '../views/ItemsEdit.vue'),
        props: true
      },
      {
        path: 'items/list',
        component: () => import(/* webpackChunkName: "ItemsList" */ '../views/ItemsList.vue')
      },
      {
        path: 'heroes/create',
        component: () => import(/* webpackChunkName: "HeroesEdit" */ '../views/HeroesEdit.vue')
      },
      {
        path: 'heroes/edit/:id',
        component: () => import(/* webpackChunkName: "HeroesEdit" */ '../views/HeroesEdit.vue'),
        props: true
      },
      {
        path: 'heroes/list',
        component: () => import(/* webpackChunkName: "HeroesList" */ '../views/HeroesList.vue')
      },
      {
        path: 'articles/create',
        component: () => import(/* webpackChunkName: "ArticlesEdit" */ '../views/ArticlesEdit.vue')
      },
      {
        path: 'articles/edit/:id',
        component: () => import(/* webpackChunkName: "ArticlesEdit" */ '../views/ArticlesEdit.vue'),
        props: true
      },
      {
        path: 'articles/list',
        component: () => import(/* webpackChunkName: "ArticlesList" */ '../views/ArticlesList.vue')
      },
      {
        path: 'ads/create',
        component: () => import(/* webpackChunkName: "AdsEdit" */ '../views/AdsEdit.vue')
      },
      {
        path: 'ads/edit/:id',
        component: () => import(/* webpackChunkName: "AdsEdit" */ '../views/AdsEdit.vue'),
        props: true
      },
      {
        path: 'ads/list',
        component: () => import(/* webpackChunkName: "AdsList" */ '../views/AdsList.vue')
      },

      {
        path: 'admin_users/create',
        component: () => import(/* webpackChunkName: "AdminUserEdit" */ '../views/AdminUserEdit.vue')
      },
      {
        path: 'admin_users/edit/:id',
        component: () => import(/* webpackChunkName: "AdminUserEdit" */ '../views/AdminUserEdit.vue'),
        props: true
      },
      {
        path: 'admin_users/list',
        component: () => import(/* webpackChunkName: "AdminUserList" */ '../views/AdminUserList.vue')
      }
    ]
  },

]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (!to.meta.isPublic && !localStorage.token) {
    return next('/login')
  }
  next()
})

export default router
